package RaceTimer;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;


public class MainMidlet extends MIDlet implements CommandListener {
    public MainMidlet() {
        initialize();
    }
    
    private Form raceEntryForm;//GEN-BEGIN:MVDFields
    private Command exitCommand;
    private Command startCommand;
    private Command stopCommand;
    private Command uploadCommand;
    private Form uploadForm;
    private Command startUploadCommand;
    private Command cancelUploadButton;
    private Form resultsForm;
    private Command resultsBackCommand;
    private TextField racerNumberTextField;
    private Command saveCommand;
    private Command recordLapTimeCommand;
    private StringItem resultsList;
    private StringItem recordedLapsStringItem;
    private Spacer spacer;
    private StringItem resultsStringItem;
    private org.netbeans.microedition.lcdui.WaitScreen uploadWaitScreen;
    private org.netbeans.microedition.util.SimpleCancellableTask uploadTask;
    private Alert uploadErrorAlert;
    private List mainMenuForm;
    private Image NewRaceImage;
    private Image ExitImage;
    private Image UploadImage;
    private Font splashFont;
    private org.netbeans.microedition.lcdui.SplashScreen splashScreen;
    private Command backCommand;
    private Command saveBeforeExitCommand;
    private Command exitWithoutSavingCommand;
    private List savedResultsForm;//GEN-END:MVDFields
    
    private Race _race;
    
    public Form getResultsForm() {
        return resultsForm;
    }
//GEN-LINE:MVDMethods
    
    /** Called by the system to indicate that a command has been invoked on a particular displayable.//GEN-BEGIN:MVDCABegin
     * @param command the Command that ws invoked
     * @param displayable the Displayable on which the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:MVDCABegin
        // Insert global pre-action code here
        if (displayable == raceEntryForm) {//GEN-BEGIN:MVDCABody
            if (command == stopCommand) {//GEN-END:MVDCABody
                // Insert pre-action code here
                // Do nothing//GEN-LINE:MVDCAAction8
                // Insert post-action code here
                getRace().stop();
            } else if (command == uploadCommand) {//GEN-LINE:MVDCACase8
                // Insert pre-action code here
                getDisplay().setCurrent(get_uploadForm());//GEN-LINE:MVDCAAction9
                // Insert post-action code here
            } else if (command == startCommand) {//GEN-LINE:MVDCACase9
                // Insert pre-action code here
                // Do nothing//GEN-LINE:MVDCAAction10
                // Insert post-action code here
                getRace().start();
            } else if (command == saveCommand) {//GEN-LINE:MVDCACase10
                // Insert pre-action code here
                // Do nothing//GEN-LINE:MVDCAAction34
                // Insert post-action code here
                RaceDataHelper.saveRace(getRace());
                getRace().save();
            } else if (command == recordLapTimeCommand) {//GEN-LINE:MVDCACase34
                // Insert pre-action code here
                // Do nothing//GEN-LINE:MVDCAAction36
                // Insert post-action code here
                try{
                    int racerNumber = Integer.parseInt(racerNumberTextField.getString());
                    //Date time = new Date();
                    Date time = getRace().recordLapTime(racerNumber);
                    recordedLapsStringItem.setText(
                            "\n" + "Race Number: " + racerNumber + " - " +
                            Utils.extractTimeString(time) +
                            recordedLapsStringItem.getText()
                            );
                    racerNumberTextField.setString("");
                } catch(NumberFormatException ex) {
                    Display.getDisplay(this).vibrate(800);
                }
            } else if (command == backCommand) {//GEN-LINE:MVDCACase36
                // Insert pre-action code here
//                if(getRace().isDirty())
//                    int iwe = 0 +2;
                // Do nothing//GEN-LINE:MVDCAAction78
                // Insert post-action code here
                //                else
                getDisplay().setCurrent(get_mainMenuForm());
            }//GEN-BEGIN:MVDCACase78
        } else if (displayable == resultsForm) {
            if (command == resultsBackCommand) {//GEN-END:MVDCACase78
                // Insert pre-action code here
                getDisplay().setCurrent(get_raceEntryForm());//GEN-LINE:MVDCAAction27
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase27
        } else if (displayable == uploadForm) {
            if (command == cancelUploadButton) {//GEN-END:MVDCACase27
                // Insert pre-action code here
                getDisplay().setCurrent(get_raceEntryForm());//GEN-LINE:MVDCAAction20
                // Insert post-action code here
            } else if (command == startUploadCommand) {//GEN-LINE:MVDCACase20
                // Insert pre-action code here
                getDisplay().setCurrent(get_uploadWaitScreen());//GEN-LINE:MVDCAAction18
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase18
        } else if (displayable == uploadWaitScreen) {
            if (command == uploadWaitScreen.FAILURE_COMMAND) {//GEN-END:MVDCACase18
                // Insert pre-action code here
                getDisplay().setCurrent(get_uploadErrorAlert(), get_uploadForm());//GEN-LINE:MVDCAAction51
                // Insert post-action code here
            } else if (command == uploadWaitScreen.SUCCESS_COMMAND) {//GEN-LINE:MVDCACase51
                // Insert pre-action code here
                getDisplay().setCurrent(get_resultsForm());//GEN-LINE:MVDCAAction50
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase50
        } else if (displayable == mainMenuForm) {
            if (command == mainMenuForm.SELECT_COMMAND) {
                switch (get_mainMenuForm().getSelectedIndex()) {
                    case 0://GEN-END:MVDCACase50
                        // Insert pre-action code here
                        getDisplay().setCurrent(get_raceEntryForm());//GEN-LINE:MVDCAAction58
                        // Insert post-action code here
                        break;//GEN-BEGIN:MVDCACase58
                    case 1://GEN-END:MVDCACase58
                        // Insert pre-action code here
                        getDisplay().setCurrent(get_savedResultsForm());//GEN-LINE:MVDCAAction64
                        // Insert post-action code here
                        break;//GEN-BEGIN:MVDCACase64
                    case 2://GEN-END:MVDCACase64
                        // Insert pre-action code here
                        exitMIDlet();//GEN-LINE:MVDCAAction61
                        // Insert post-action code here
                        break;//GEN-BEGIN:MVDCACase61
                }
            }
        } else if (displayable == splashScreen) {
            if (command == splashScreen.DISMISS_COMMAND) {//GEN-END:MVDCACase61
                // Insert pre-action code here
                getDisplay().setCurrent(get_mainMenuForm());//GEN-LINE:MVDCAAction74
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase74
        } else if (displayable == savedResultsForm) {
            if (command == backCommand) {//GEN-END:MVDCACase74
                // Insert pre-action code here
                getDisplay().setCurrent(get_mainMenuForm());//GEN-LINE:MVDCAAction90
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase90
        }//GEN-END:MVDCACase90
        // Insert global post-action code here
}//GEN-LINE:MVDCAEnd
    
    /** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin
     */
    private void initialize() {//GEN-END:MVDInitBegin
        // Insert pre-init code here
        backCommand = new Command("Back", Command.BACK, 1);//GEN-BEGIN:MVDInitInit
        getDisplay().setCurrent(get_splashScreen());//GEN-END:MVDInitInit
        // Insert post-init code here
        setRace(new Race());
    }//GEN-LINE:MVDInitEnd
    
    /**
     * This method should return an instance of the display.
     */
    public Display getDisplay()//GEN-FIRST:MVDGetDisplay
{
        return Display.getDisplay(this);
    }//GEN-LAST:MVDGetDisplay
    
    /**
     * This method should exit the midlet.
     */
    public void exitMIDlet()//GEN-FIRST:MVDExitMidlet
{
        getDisplay().setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }//GEN-LAST:MVDExitMidlet
    
    /** This method returns instance for raceEntryForm component and should be called instead of accessing raceEntryForm field directly.//GEN-BEGIN:MVDGetBegin2
     * @return Instance for raceEntryForm component
     */
    public Form get_raceEntryForm() {
        if (raceEntryForm == null) {//GEN-END:MVDGetBegin2
            // Insert pre-init code here
            raceEntryForm = new Form("Race Timer", new Item[] {//GEN-BEGIN:MVDGetInit2
                get_racerNumberTextField(),
                get_spacer(),
                get_recordedLapsStringItem()
            });
            raceEntryForm.addCommand(get_recordLapTimeCommand());
            raceEntryForm.addCommand(get_startCommand());
            raceEntryForm.addCommand(get_stopCommand());
            raceEntryForm.addCommand(get_uploadCommand());
            raceEntryForm.addCommand(get_saveCommand());
            raceEntryForm.addCommand(backCommand);
            raceEntryForm.setCommandListener(this);//GEN-END:MVDGetInit2
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd2
        return raceEntryForm;
    }//GEN-END:MVDGetEnd2
    
    /** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.//GEN-BEGIN:MVDGetBegin3
     * @return Instance for exitCommand component
     */
    public Command get_exitCommand() {
        if (exitCommand == null) {//GEN-END:MVDGetBegin3
            // Insert pre-init code here
            exitCommand = new Command("Exit", Command.EXIT, 1);//GEN-LINE:MVDGetInit3
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd3
        return exitCommand;
    }//GEN-END:MVDGetEnd3
    
    /** This method returns instance for startCommand component and should be called instead of accessing startCommand field directly.//GEN-BEGIN:MVDGetBegin4
     * @return Instance for startCommand component
     */
    public Command get_startCommand() {
        if (startCommand == null) {//GEN-END:MVDGetBegin4
            // Insert pre-init code here
            startCommand = new Command("Start", Command.ITEM, 1);//GEN-LINE:MVDGetInit4
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd4
        return startCommand;
    }//GEN-END:MVDGetEnd4
    
    /** This method returns instance for stopCommand component and should be called instead of accessing stopCommand field directly.//GEN-BEGIN:MVDGetBegin5
     * @return Instance for stopCommand component
     */
    public Command get_stopCommand() {
        if (stopCommand == null) {//GEN-END:MVDGetBegin5
            // Insert pre-init code here
            stopCommand = new Command("Stop", Command.ITEM, 1);//GEN-LINE:MVDGetInit5
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd5
        return stopCommand;
    }//GEN-END:MVDGetEnd5
    
    /** This method returns instance for uploadCommand component and should be called instead of accessing uploadCommand field directly.//GEN-BEGIN:MVDGetBegin6
     * @return Instance for uploadCommand component
     */
    public Command get_uploadCommand() {
        if (uploadCommand == null) {//GEN-END:MVDGetBegin6
            // Insert pre-init code here
            uploadCommand = new Command("Upload", Command.ITEM, 1);//GEN-LINE:MVDGetInit6
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd6
        return uploadCommand;
    }//GEN-END:MVDGetEnd6
    
    /** This method returns instance for uploadForm component and should be called instead of accessing uploadForm field directly.//GEN-BEGIN:MVDGetBegin14
     * @return Instance for uploadForm component
     */
    public Form get_uploadForm() {
        if (uploadForm == null) {//GEN-END:MVDGetBegin14
            // Insert pre-init code here
            uploadForm = new Form("Upload Results", new Item[] {get_resultsList()});//GEN-BEGIN:MVDGetInit14
            uploadForm.addCommand(get_startUploadCommand());
            uploadForm.addCommand(get_cancelUploadButton());
            uploadForm.setCommandListener(this);//GEN-END:MVDGetInit14
            // Insert post-init code here
            
            Vector v = getRace().getLapTimes();
            
            for (int i=0;i<v.size();i++) {
                LapTime lapTime = (LapTime) v.elementAt(i);
                resultsList.setText(resultsList.getText() + "\nRacer: " +
                        lapTime.getRacerNumber() + " Time: " +
                        Utils.extractTimeString(lapTime.getTime()) +
                        " Lap: " + lapTime.getLapNumber());
            }
        }//GEN-BEGIN:MVDGetEnd14
        return uploadForm;
    }//GEN-END:MVDGetEnd14
    
    /** This method returns instance for startUploadCommand component and should be called instead of accessing startUploadCommand field directly.//GEN-BEGIN:MVDGetBegin17
     * @return Instance for startUploadCommand component
     */
    public Command get_startUploadCommand() {
        if (startUploadCommand == null) {//GEN-END:MVDGetBegin17
            // Insert pre-init code here
            startUploadCommand = new Command("Start Upload", Command.ITEM, 1);//GEN-LINE:MVDGetInit17
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd17
        return startUploadCommand;
    }//GEN-END:MVDGetEnd17
    
    /** This method returns instance for cancelUploadButton component and should be called instead of accessing cancelUploadButton field directly.//GEN-BEGIN:MVDGetBegin19
     * @return Instance for cancelUploadButton component
     */
    public Command get_cancelUploadButton() {
        if (cancelUploadButton == null) {//GEN-END:MVDGetBegin19
            // Insert pre-init code here
            cancelUploadButton = new Command("Cancel", Command.CANCEL, 1);//GEN-LINE:MVDGetInit19
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd19
        return cancelUploadButton;
    }//GEN-END:MVDGetEnd19
    
    
    /** This method returns instance for resultsForm component and should be called instead of accessing resultsForm field directly.//GEN-BEGIN:MVDGetBegin24
     * @return Instance for resultsForm component
     */
    public Form get_resultsForm() {
        if (resultsForm == null) {//GEN-END:MVDGetBegin24
            // Insert pre-init code here
            resultsForm = new Form("Results", new Item[] {get_resultsStringItem()});//GEN-BEGIN:MVDGetInit24
            resultsForm.addCommand(get_resultsBackCommand());
            resultsForm.setCommandListener(this);//GEN-END:MVDGetInit24
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd24
        return resultsForm;
    }//GEN-END:MVDGetEnd24
    
    
    /** This method returns instance for resultsBackCommand component and should be called instead of accessing resultsBackCommand field directly.//GEN-BEGIN:MVDGetBegin26
     * @return Instance for resultsBackCommand component
     */
    public Command get_resultsBackCommand() {
        if (resultsBackCommand == null) {//GEN-END:MVDGetBegin26
            // Insert pre-init code here
            resultsBackCommand = new Command("Back", Command.BACK, 1);//GEN-LINE:MVDGetInit26
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd26
        return resultsBackCommand;
    }//GEN-END:MVDGetEnd26
    
    /** This method returns instance for racerNumberTextField component and should be called instead of accessing racerNumberTextField field directly.//GEN-BEGIN:MVDGetBegin30
     * @return Instance for racerNumberTextField component
     */
    public TextField get_racerNumberTextField() {
        if (racerNumberTextField == null) {//GEN-END:MVDGetBegin30
            // Insert pre-init code here
            racerNumberTextField = new TextField("Racer Number:", "", 120, TextField.NUMERIC);//GEN-LINE:MVDGetInit30
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd30
        return racerNumberTextField;
    }//GEN-END:MVDGetEnd30
    
    /** This method returns instance for saveCommand component and should be called instead of accessing saveCommand field directly.//GEN-BEGIN:MVDGetBegin32
     * @return Instance for saveCommand component
     */
    public Command get_saveCommand() {
        if (saveCommand == null) {//GEN-END:MVDGetBegin32
            // Insert pre-init code here
            saveCommand = new Command("Save", Command.ITEM, 1);//GEN-LINE:MVDGetInit32
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd32
        return saveCommand;
    }//GEN-END:MVDGetEnd32
    
    /** This method returns instance for recordLapTimeCommand component and should be called instead of accessing recordLapTimeCommand field directly.//GEN-BEGIN:MVDGetBegin35
     * @return Instance for recordLapTimeCommand component
     */
    public Command get_recordLapTimeCommand() {
        if (recordLapTimeCommand == null) {//GEN-END:MVDGetBegin35
            // Insert pre-init code here
            recordLapTimeCommand = new Command("Record Lap Time", Command.OK, 1);//GEN-LINE:MVDGetInit35
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd35
        return recordLapTimeCommand;
    }//GEN-END:MVDGetEnd35
    
    /** This method returns instance for resultsList component and should be called instead of accessing resultsList field directly.//GEN-BEGIN:MVDGetBegin37
     * @return Instance for resultsList component
     */
    public StringItem get_resultsList() {
        if (resultsList == null) {//GEN-END:MVDGetBegin37
            // Insert pre-init code here
            resultsList = new StringItem("Results\n", "");//GEN-LINE:MVDGetInit37
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd37
        return resultsList;
    }//GEN-END:MVDGetEnd37
    
    
    /** This method returns instance for recordedLapsStringItem component and should be called instead of accessing recordedLapsStringItem field directly.//GEN-BEGIN:MVDGetBegin40
     * @return Instance for recordedLapsStringItem component
     */
    public StringItem get_recordedLapsStringItem() {
        if (recordedLapsStringItem == null) {//GEN-END:MVDGetBegin40
            // Insert pre-init code here
            recordedLapsStringItem = new StringItem("Recorded laps", "");//GEN-LINE:MVDGetInit40
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd40
        return recordedLapsStringItem;
    }//GEN-END:MVDGetEnd40
    
    /** This method returns instance for spacer component and should be called instead of accessing spacer field directly.//GEN-BEGIN:MVDGetBegin41
     * @return Instance for spacer component
     */
    public Spacer get_spacer() {
        if (spacer == null) {//GEN-END:MVDGetBegin41
            // Insert pre-init code here
            spacer = new Spacer(1000, 1);//GEN-LINE:MVDGetInit41
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd41
        return spacer;
    }//GEN-END:MVDGetEnd41
    
    /** This method returns instance for resultsStringItem component and should be called instead of accessing resultsStringItem field directly.//GEN-BEGIN:MVDGetBegin43
     * @return Instance for resultsStringItem component
     */
    public StringItem get_resultsStringItem() {
        if (resultsStringItem == null) {//GEN-END:MVDGetBegin43
            // Insert pre-init code here
            resultsStringItem = new StringItem("", "");//GEN-LINE:MVDGetInit43
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd43
        return resultsStringItem;
    }//GEN-END:MVDGetEnd43
    
    /** This method returns instance for uploadWaitScreen component and should be called instead of accessing uploadWaitScreen field directly.//GEN-BEGIN:MVDGetBegin49
     * @return Instance for uploadWaitScreen component
     */
    public org.netbeans.microedition.lcdui.WaitScreen get_uploadWaitScreen() {
        if (uploadWaitScreen == null) {//GEN-END:MVDGetBegin49
            // Insert pre-init code here
            uploadWaitScreen = new org.netbeans.microedition.lcdui.WaitScreen(getDisplay());//GEN-BEGIN:MVDGetInit49
            uploadWaitScreen.setCommandListener(this);
            uploadWaitScreen.setFullScreenMode(true);
            uploadWaitScreen.setText("Uploading ...");
            uploadWaitScreen.setTask(get_uploadTask());//GEN-END:MVDGetInit49
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd49
        return uploadWaitScreen;
    }//GEN-END:MVDGetEnd49
    
    /** This method returns instance for uploadTask component and should be called instead of accessing uploadTask field directly.//GEN-BEGIN:MVDGetBegin52
     * @return Instance for uploadTask component
     */
    public org.netbeans.microedition.util.SimpleCancellableTask get_uploadTask() {
        if (uploadTask == null) {//GEN-END:MVDGetBegin52
            // Insert pre-init code here
            
            uploadTask = new org.netbeans.microedition.util.SimpleCancellableTask();//GEN-BEGIN:MVDGetInit52
            uploadTask.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {
                    HttpConnection connection = null;
                    
                    String type = "application/x-www-form-urlencoded";
                    StringBuffer responseBuffer = new StringBuffer();;
                    String results = _race.serializeResults();
                    
                    connection = (HttpConnection) Connector.open(getAppProperty("UploadUrl"));
                    connection.setRequestMethod( HttpConnection.POST );
                    connection.setRequestProperty( "Content-Type", type );
                    connection.setRequestProperty( "Content-Length",
                            String.valueOf(results.length()) );
                    
                    OutputStream requestStrem = connection.openOutputStream();
                    requestStrem.write(results.getBytes());
                    requestStrem.close();
                    
                    int responseCode = connection.getResponseCode();
                    
                    DataInputStream responseStream = connection.openDataInputStream();
                    
                    int ch;
                    while((ch = responseStream.read()) != -1) {
                        responseBuffer.append((char) ch);
                    }
                    
                    get_resultsForm().append(responseBuffer.toString());
                }
            });//GEN-END:MVDGetInit52
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd52
        return uploadTask;
    }//GEN-END:MVDGetEnd52
    
    /** This method returns instance for uploadErrorAlert component and should be called instead of accessing uploadErrorAlert field directly.//GEN-BEGIN:MVDGetBegin53
     * @return Instance for uploadErrorAlert component
     */
    public Alert get_uploadErrorAlert() {
        if (uploadErrorAlert == null) {//GEN-END:MVDGetBegin53
            // Insert pre-init code here
            uploadErrorAlert = new Alert("Error", "Results Failed to upload", null, AlertType.ERROR);//GEN-BEGIN:MVDGetInit53
            uploadErrorAlert.setTimeout(-2);//GEN-END:MVDGetInit53
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd53
        return uploadErrorAlert;
    }//GEN-END:MVDGetEnd53
    
    /** This method returns instance for mainMenuForm component and should be called instead of accessing mainMenuForm field directly.//GEN-BEGIN:MVDGetBegin55
     * @return Instance for mainMenuForm component
     */
    public List get_mainMenuForm() {
        if (mainMenuForm == null) {//GEN-END:MVDGetBegin55
            // Insert pre-init code here
            mainMenuForm = new List(null, Choice.IMPLICIT, new String[] {//GEN-BEGIN:MVDGetInit55
                "New Race",
                "View Saved Results",
                "Exit"
            }, new Image[] {
                get_NewRaceImage(),
                get_UploadImage(),
                get_ExitImage()
            });
            mainMenuForm.setCommandListener(this);
            mainMenuForm.setSelectedFlags(new boolean[] {
                false,
                false,
                false
            });//GEN-END:MVDGetInit55
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd55
        return mainMenuForm;
    }//GEN-END:MVDGetEnd55
    
    /** This method returns instance for NewRaceImage component and should be called instead of accessing NewRaceImage field directly.//GEN-BEGIN:MVDGetBegin59
     * @return Instance for NewRaceImage component
     */
    public Image get_NewRaceImage() {
        if (NewRaceImage == null) {//GEN-END:MVDGetBegin59
            // Insert pre-init code here
            try {//GEN-BEGIN:MVDGetInit59
                NewRaceImage = Image.createImage("/RaceTimer/Images/clock.png");
            } catch (java.io.IOException exception) {
                exception.printStackTrace();
            }//GEN-END:MVDGetInit59
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd59
        return NewRaceImage;
    }//GEN-END:MVDGetEnd59
    
    /** This method returns instance for ExitImage component and should be called instead of accessing ExitImage field directly.//GEN-BEGIN:MVDGetBegin62
     * @return Instance for ExitImage component
     */
    public Image get_ExitImage() {
        if (ExitImage == null) {//GEN-END:MVDGetBegin62
            // Insert pre-init code here
            try {//GEN-BEGIN:MVDGetInit62
                ExitImage = Image.createImage("/RaceTimer/Images/whistle.png");
            } catch (java.io.IOException exception) {
                exception.printStackTrace();
            }//GEN-END:MVDGetInit62
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd62
        return ExitImage;
    }//GEN-END:MVDGetEnd62
    
    /** This method returns instance for UploadImage component and should be called instead of accessing UploadImage field directly.//GEN-BEGIN:MVDGetBegin65
     * @return Instance for UploadImage component
     */
    public Image get_UploadImage() {
        if (UploadImage == null) {//GEN-END:MVDGetBegin65
            // Insert pre-init code here
            try {//GEN-BEGIN:MVDGetInit65
                UploadImage = Image.createImage("/RaceTimer/Images/upload.png");
            } catch (java.io.IOException exception) {
                exception.printStackTrace();
            }//GEN-END:MVDGetInit65
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd65
        return UploadImage;
    }//GEN-END:MVDGetEnd65
    
    /** This method returns instance for splashFont component and should be called instead of accessing splashFont field directly.//GEN-BEGIN:MVDGetBegin70
     * @return Instance for splashFont component
     */
    public Font get_splashFont() {
        if (splashFont == null) {//GEN-END:MVDGetBegin70
            // Insert pre-init code here
            splashFont = Font.getFont(Font.FACE_PROPORTIONAL, 0x3, Font.SIZE_LARGE);//GEN-LINE:MVDGetInit70
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd70
        return splashFont;
    }//GEN-END:MVDGetEnd70
    
    /** This method returns instance for splashScreen component and should be called instead of accessing splashScreen field directly.//GEN-BEGIN:MVDGetBegin73
     * @return Instance for splashScreen component
     */
    public org.netbeans.microedition.lcdui.SplashScreen get_splashScreen() {
        if (splashScreen == null) {//GEN-END:MVDGetBegin73
            // Insert pre-init code here
            splashScreen = new org.netbeans.microedition.lcdui.SplashScreen(getDisplay());//GEN-BEGIN:MVDGetInit73
            splashScreen.setCommandListener(this);
            splashScreen.setText("Mobile Race Timer v 0.1");
            splashScreen.setTextFont(get_splashFont());
            splashScreen.setTimeout(3000);//GEN-END:MVDGetInit73
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd73
        return splashScreen;
    }//GEN-END:MVDGetEnd73
    
    
    
    /** This method returns instance for saveBeforeExitCommand component and should be called instead of accessing saveBeforeExitCommand field directly.//GEN-BEGIN:MVDGetBegin82
     * @return Instance for saveBeforeExitCommand component
     */
    public Command get_saveBeforeExitCommand() {
        if (saveBeforeExitCommand == null) {//GEN-END:MVDGetBegin82
            // Insert pre-init code here
            saveBeforeExitCommand = new Command("Save", Command.ITEM, 1);//GEN-LINE:MVDGetInit82
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd82
        return saveBeforeExitCommand;
    }//GEN-END:MVDGetEnd82
    
    /** This method returns instance for exitWithoutSavingCommand component and should be called instead of accessing exitWithoutSavingCommand field directly.//GEN-BEGIN:MVDGetBegin84
     * @return Instance for exitWithoutSavingCommand component
     */
    public Command get_exitWithoutSavingCommand() {
        if (exitWithoutSavingCommand == null) {//GEN-END:MVDGetBegin84
            // Insert pre-init code here
            exitWithoutSavingCommand = new Command("Exit without saving", Command.OK, 1);//GEN-LINE:MVDGetInit84
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd84
        return exitWithoutSavingCommand;
    }//GEN-END:MVDGetEnd84
    
    /** This method returns instance for savedResultsForm component and should be called instead of accessing savedResultsForm field directly.//GEN-BEGIN:MVDGetBegin88
     * @return Instance for savedResultsForm component
     */
    public List get_savedResultsForm() {
        if (savedResultsForm == null) {//GEN-END:MVDGetBegin88
            // Insert pre-init code here
            savedResultsForm = new List(null, Choice.IMPLICIT, new String[0], new Image[0]);//GEN-BEGIN:MVDGetInit88
            savedResultsForm.addCommand(backCommand);
            savedResultsForm.setCommandListener(this);
            savedResultsForm.setSelectedFlags(new boolean[0]);//GEN-END:MVDGetInit88
            // Insert post-init code here
        }
        try {
            StoredResult[] results = RaceDataHelper.getStoredResutls();
            for(int i=0; i < results.length;i++) {
                savedResultsForm.append(results[i].getDate().toString(),null);
            }
            
        } catch (Exception e) {
        }//GEN-BEGIN:MVDGetEnd88
        return savedResultsForm;
    }//GEN-END:MVDGetEnd88
    
    
    public void startApp() {
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public Race getRace() {
        return _race;
    }
    
    public void setRace(Race race) {
        this._race = race;
    }
    
}
